from Interpolation import Interpolation, run

class EquidistantNewton(Interpolation):
    def __init__(self, h = 10):
        super().__init__()
        self.h = h
        self.diff = []
    def addKey(self, x, y):
        super().addKey(self.h * len(self.keys), y)
        thisRow = [y]
        if self.diff:
            for y in self.diff[-1]:
                thisRow.append(thisRow[-1]-y)
        self.diff.append(thisRow)
        print(thisRow)
        # update fn
        def factorial(n):
            prod = 1
            for i in range(1, n+1):
                prod *= i
            return prod
        def C(n, t):
            prod = 1
            for i in range(n):
                prod *= (t-i)
            return prod / factorial(n)
        def fn(x):
            sum = 0
            t = x / self.h
            for i in range(len(self.diff)):
                sum += C(i, t) * self.diff[i][i]
            return sum
        self.compute = fn

run(EquidistantNewton(20))